package com.mega32k.mevila.block;

import com.mega32k.mevila.block.tileentity.AlloyInlayTER;
import com.mega32k.mevila.util.BlockInit;
import com.mega32k.mevila.util.ItemInit;
import com.mega32k.mevila.util.TileEntityTypeRegistry;
import javafx.beans.property.BooleanProperty;
import net.minecraft.block.*;
import net.minecraft.block.material.Material;
import net.minecraft.block.material.MaterialColor;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.tileentity.ItemStackTileEntityRenderer;
import net.minecraft.entity.item.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemModelsProperties;
import net.minecraft.item.ItemStack;
import net.minecraft.loot.conditions.EntityHasProperty;
import net.minecraft.particles.ParticleTypes;
import net.minecraft.state.IntegerProperty;
import net.minecraft.state.StateContainer;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.shapes.ISelectionContext;
import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.util.math.shapes.VoxelShapes;
import net.minecraft.world.Blockreader;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.ToolType;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import javax.annotation.Nullable;
import java.util.Random;
import java.util.Timer;

@Mod.EventBusSubscriber
public class AlloyInlaySBlock extends Block{
    private static IntegerProperty ROTATE = IntegerProperty.create("rotate", 0, 360);

    public AlloyInlaySBlock() {
        super(AbstractBlock.Properties
                .create(Material.IRON, MaterialColor.IRON)
                .harvestTool(ToolType.PICKAXE)
                .hardnessAndResistance(5.0F, 6.0F)
                .sound(SoundType.METAL)
        );
    }

    @Override
    protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
        builder.add(ROTATE);
        super.fillStateContainer(builder);
    }

    @Override
    public boolean hasTileEntity(BlockState state) {
        return true;
    }

    private static VoxelShape shape;

    static {
        VoxelShape base = Block.makeCuboidShape(0, 0, 0, 16, 1, 16);
        VoxelShape column1 = Block.makeCuboidShape(0, 1, 0, 1, 15, 1);
        VoxelShape column2 = Block.makeCuboidShape(15, 1, 0, 16, 15, 1);
        VoxelShape column3 = Block.makeCuboidShape(0, 1, 15, 1, 15, 16);
        VoxelShape column4 = Block.makeCuboidShape(15, 1, 15, 16, 15, 16);
        VoxelShape top = Block.makeCuboidShape(0, 15, 0, 16, 16, 16);
        shape = VoxelShapes.or(base, column1, column2, column3, column4, top);
    }

    @Override
    public VoxelShape getShape(BlockState p_220053_1_, IBlockReader p_220053_2_, BlockPos p_220053_3_, ISelectionContext p_220053_4_) {
        return shape;
    }


    @Override
    public ActionResultType onBlockActivated(BlockState blockState, World world, BlockPos blockPos, PlayerEntity player, Hand hand, BlockRayTraceResult blockRayTraceResult) {
        if (player.getHeldItemMainhand() != null)
            AlloyInlayTERTileEntity.setItemStack(player.getHeldItemMainhand());
        return ActionResultType.SUCCESS;
    }

    @Nullable
    @Override
    public TileEntity createTileEntity(BlockState state, IBlockReader world) {
        return new AlloyInlayTERTileEntity();
    }

    public static class AlloyInlayTERTileEntity extends TileEntity implements ITickableTileEntity {
        private static int i;
        public static ItemStack itemStack2 = new ItemStack(ItemInit.MEVILA_SWORD_ITEM_REGISTRY_OBJECT.get());
        public AlloyInlaySBlock alloyInlaySBlock;

        public AlloyInlayTERTileEntity() {
            super(TileEntityTypeRegistry.alooyInlayTERTileEntity.get());
        }

        public static void setItemStack(ItemStack itemStack) {
            itemStack2 = itemStack;
        }


        public static ItemStack getStack() {
            return itemStack2;
        }
        @Override
        public void tick() {
            if (world != null && this.getTileEntity() != null) {
                setItemStack(getStack());
            }
        }
    }

}
